/*
 * ClkPro.cpp
 *
 * Created: 3/18/2013 11:22:57 PM
 *  Author: Ozan
 */ 

# define F_CPU 16000000L
#include <avr/io.h>
#include <util/delay.h>    // including the avr delay lib
#include "usart.h"			// Header for Serial communication
#include "outp.h"

//#include "stdint.h"
//#include "inttypes.h"


OUTP led1(0x05,5);			// Creates an object called led1 connected to PortB.5 (0x05 is the Special function register for PORTB)
OUTP trigPin(0x05,4);
OUTP led(0x05,3);
OUTP led2(0x05,2);

void senseDistance();
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);

int main(void)
{
	// INITIALIZATIONS
	DDRB = 0b00111100;	//B5 output: board LED
	USART_Init(MYUBRR); // Initializes the serial communication
	// Go to USART.H AND CHANGE YOUR FOSC AND BAUD
	USART_Send_string("Clk-Pro\n");
    while(1)
    {
        //TODO:: Please write your application code 
		led1.on();
		USART_Send_string("System Scan\n");
		_delay_ms(1000);
		led1.off();
		_delay_ms(1000);
		senseDistance();
    }
}

void senseDistance()
{
	long duration, distance;
	trigPin.off();  // Added this line
	_delay_ms(2); // Added this line
	trigPin.on();
	//  delayMicroseconds(1000); - Removed this line
	_delay_ms(10); // Added this line
	trigPin.off();
	duration = pulseIn(PORTB1,1,1000);
	distance = (duration/2) / 29.1;
	if (distance < 4)
	{  // This is where the LED On/Off happens
		led.on(); // When the Red condition is met, the Green LED should turn off
		led2.off();
	}
	else
	{
		led.off();
		led2.on();
	}
	if (distance >= 200 || distance <= 0)
	{
		USART_Send_string("Out of range");
	}
	else
	{
		USART_Send_string("" + distance);
		USART_Send_string(" cm");
	}
	_delay_ms(500);
}

unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
{
	// cache the port and bit of the pin in order to speed up the
	// pulse width measuring loop and achieve finer resolution.  calling
	// digitalRead() instead yields much coarser resolution.
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);
	uint8_t stateMask = (state ? bit : 0);
	unsigned long width = 0; // keep initialization out of time critical area
	
	// convert the timeout from microseconds to a number of times through
	// the initial loop; it takes 16 clock cycles per iteration.
	unsigned long numloops = 0;
	unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
	
	// wait for any previous pulse to end
	while ((*portInputRegister(port) & bit) == stateMask)
	if (numloops++ == maxloops)
	return 0;
	
	// wait for the pulse to start
	while ((*portInputRegister(port) & bit) != stateMask)
	if (numloops++ == maxloops)
	return 0;
	
	// wait for the pulse to stop
	while ((*portInputRegister(port) & bit) == stateMask) {
		if (numloops++ == maxloops)
		return 0;
		width++;
	}

	// convert the reading to microseconds. The loop has been determined
	// to be 20 clock cycles long and have about 16 clocks between the edge
	// and the start of the loop. There will be some error introduced by
	// the interrupt handlers.
	return clockCyclesToMicroseconds(width * 21 + 16);
}